package com.example.blood_sugar.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.blood_sugar.R;
import com.example.blood_sugar.bean.BSRecord;
import com.example.blood_sugar.db.BSRecordHolder;
import com.example.blood_sugar.utils.DateUtil;
import com.example.blood_sugar.view.LineGraphicView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 和BloodPressureChartFragment差不多，显示30条记录的曲线图
 */
public class HbAlcChartFragment extends BaseFragment {

    private ArrayList<Double> yList1;

    private ArrayList<String> xRawDatas;
    private LineGraphicView mLineGraphicView1;

    private List<BSRecord> mBSRecords;

    private int count = 30;

    public static HbAlcChartFragment newInstance(String text){
        Bundle bundle = new Bundle();
        bundle.putString("text",text);
        HbAlcChartFragment bloodPressureChartFragment = new HbAlcChartFragment();
        bloodPressureChartFragment.setArguments(bundle);
        return  bloodPressureChartFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_glycated_hemoglobin_chart, container, false);
        mLineGraphicView1 = view.findViewById(R.id.line_graphic1);

        yList1 = new ArrayList<>();

        xRawDatas = new ArrayList<>();
        for (int i = 0; i < count; i ++) {
            xRawDatas.add(DateUtil.date2Str(DateUtil.getPreDate(new Date(), count - 1 - i), "MM月dd日"));
        }

        initView();

        return view;
    }

    public void initView(){
        if (yList1 == null)
            return;
        mBSRecords = BSRecordHolder.get(getContext()).getBSRecords(count);
        if (yList1.size() > 0){
            yList1.clear();
        }
        for (int i = 0; i < count; i ++){
            BSRecord bsRecord = mBSRecords.get(i);
            yList1.add((double) bsRecord.getSaccharificationValue());
        }

        mLineGraphicView1.setData(yList1, xRawDatas, 30, 5);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // TODO: 2018/8/21 创建成功后初始化操作？
    }

    @Override
    public void onResume() {
        super.onResume();
        initView();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        initView();
    }
}
